//
//  JJWelcomeViewController.swift
//  微博1
//
//  Created by 卷卷 on 15/9/8.
//  Copyright © 2015年 JJ. All rights reserved.
//

import UIKit
import SDWebImage

class JJWelcomeViewController: UIViewController {

    //头像底部约束
    private var iconBottomCons: NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        //布局空间
        setupUI()
        
        //设置用户头像
        iconView.sd_setImageWithURL(JJUserAccountViewModel.shareUserAccount.avatarUrl)
    
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        // 开始动画
        // 1. 计算目标的约束数值
        let h = -(UIScreen.mainScreen().bounds.height + iconBottomCons!.constant)
        // 2. 修改约束数值
        // 使用自动布局，苹果提供了一个自动布局系统，在后台维护界面元素的位置和大小
        // 一旦使用了自动布局，就不要在直接设置 frame
        // 自动布局系统，会`收集`界面上所有需要重新调正位置/大小的控件的约束，然后一次性修改
        // 如果开发中需要强行更新约束，可以直接调用 layoutIfNeeded 方法，会将当前所有的约束变化应用到控件上
        iconBottomCons?.constant = h
        
        // 3. 开始动画
        label.alpha = 0
        // 开发中需要注意：不要让重要的控件移出屏幕外侧！
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            
            // 如果需要更新布局
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                
                UIView.animateWithDuration(0.8, animations: { () -> Void in
                    self.label.alpha = 1
                    }, completion: { (_) -> Void in
                        
                        // 提示：程序中尽量不要直接在其他位置更新根控制器
                        // UIApplication.sharedApplication().keyWindow?.rootViewController = MainViewController()
                        // 利用通知，通知 AppDelegate 更改控制器
                        NSNotificationCenter.defaultCenter().postNotificationName(JJSwitchRootViewControllerNotification, object: nil)
        })
     }

    }
    
   //设置控件
    private func setupUI() {
        
        //1.添加控件
        view.addSubview(backImageView)
        view.addSubview(iconView)
        view.addSubview(label)
        
        //2.自动布局
        backImageView.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[V]-0-|", options: [], metrics: nil, views: ["v":backImageView]))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[v]-0-|", options: [], metrics: nil, views: ["v":backImageView]))
        
        //纪录垂直方向的约束
        //将新建的约束，添加到视图的约束数组中
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -200))
        self.iconBottomCons = view.constraints.last
        
        // 设定头像的宽高约束
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 90))
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 90))
        
        label.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
     
    }
    

    // MARK : - 懒加载
    private lazy var backImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    private lazy var iconView: UIImageView = {
        
        let iv = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        //设置圆角
        iv.layer.cornerRadius = 45
        iv.layer.masksToBounds = true
        
        return iv
    }()
    
    private  lazy var label:UILabel = {
        
        let label = UILabel()
        
        label.text = "欢迎归来"
        
        return label
    }()
}
